extends Bullet


@onready var timer_trigger: TimerTrigger = $TimerTrigger


@export var bullet_factory: INodeFactory = null
## 发射子弹的重定向设置
@export var settings: Array[PackedStateResource] = []


var source: ProjectileResource = null


func __check_exports() -> Dictionary:
    return IComponent.merge({
        "bullet_factory": IComponent.find_down.bind(self, func(x): return x is INodeFactory, bullet_factory),
    }, super.__check_exports())

func __set_enable(v: bool) -> void:
    super.__set_enable(v)
    if not v:
        timer_trigger.is_enable = false

func setup(arg: ProjectileResource) -> void:
    super.setup(arg)

    source = arg
    timer_trigger.is_enable = true
    shoot()

func shoot() -> void:
    for state in settings:
        source.start_position = position
        var b: Projectile = HitRedirect.spawn(source, state, weapon, bullet_factory)
        b.event_destroy.connect(__erase)

func __erase(bullet: Projectile) -> void:
    bullet.event_destroy.disconnect(__erase)
    bullet_factory.destroy(bullet)
